﻿using System.ComponentModel.Composition;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Layout;
using Security.Helpers;
using Security.Specs.Common;

namespace Security.Events
{
    /// <summary>
    /// <see cref="ILogger"/>
    /// </summary>
    [Export(typeof(ILogger))]
    internal class Logger : ILogger
    {
        private readonly ILog log = LogManager.GetLogger(typeof(Logger));

        public Logger()
        {
            ConfigureLogger();
        }

        private static void ConfigureLogger()
        {
            var consoleAppender = new ColoredConsoleAppender();
            consoleAppender.Layout = new PatternLayout(@"[%d] %5p : %m%n");
            BasicConfigurator.Configure(consoleAppender);
            consoleAppender.ActivateOptions();
        }

        public void WriteLog(string message)
        {
            log.Debug(message);
        }

        public void WriteLog(ErrorSeverity severity, string message)
        {
            severity.Match(ErrorSeverity.Error, () => log.Error(message)).
                Match(ErrorSeverity.Warning, () => log.Warn(message)).
                Match(ErrorSeverity.Info, () => log.Info(message));
        }
    }
}
